package cn.itcast.controller.system;

import cn.itcast.base.BaseController;
import cn.itcast.domain.module.Module;
import cn.itcast.service.system.ModuleService;
import cn.itcast.utils.commons.UtilFuns;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = "/system/module")
public class ModuleController extends BaseController {
    @Autowired
    private ModuleService moduleService;

    @RequestMapping("/list.do")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "4") int pageSize) {

        PageInfo pageInfo = moduleService.findAllPage(pageNum, pageSize);
        request.setAttribute("page", pageInfo);
        return "system/module/module-list";
    }

    @RequestMapping(value = "/edit")
    public String edit(Module module) {

        if (UtilFuns.isEmpty(module.getId())) {
            module.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            moduleService.save(module);
        } else {
            moduleService.update(module);
        }

        return "redirect:/system/module/list.do";

    }
    @RequestMapping(value = "/delete")
    public String delete(String id) {

        moduleService.delete(id);
        return "redirect:/system/module/list.do";
    }
    @RequestMapping(value = "/toUpdate",name = "菜单编辑页面")
    public String toUpdate(String id) {
        Module module = moduleService.findById(id);
        request.setAttribute("module",module);
        //带着所有的一级、二级菜单过去
        List<Module> moduleList = moduleService.findModule();
        request.setAttribute("menus",moduleList);
        return "system/module/module-update";
    }
    @RequestMapping(value = "/toAdd",name = "菜单的新增页面")
    public String toAdd() {
        List<Module> module = moduleService.findModule();
        request.setAttribute("menus",module);
        return "system/module/module-add";
    }
}
